<template>
  <div id="userRegisterPage">
    <h2 class="title">时光小馆：客官注册一下噻！</h2>
    <div class="desc">欢迎~光临~！</div>
    <a-form :model="formState" name="basic" autocomplete="off" @finish="handleSubmit">
      <a-form-item
        label="输入账号："
        name="userAccount"
        :rules="[{ required: true, message: '喂喂喂，输入账号噻！' }]"
      >
        <a-input v-model:value="formState.userAccount" placeholder="来，写个账号吧"/>
      </a-form-item>

      <a-form-item
        label="输入密码："
        name="userPassword"
        :rules="[
          { required: true, message: '喂喂喂，密码也要输入呀。' },
          { min: 8, message: '密码长度不能小于8位哦' },
        ]"
      >
        <a-input-password v-model:value="formState.userPassword" placeholder="来，输个密码吧"/>
      </a-form-item>

      <a-form-item
        label="确认密码："
        name="checkPassword"
        :rules="[
          { required: true, message: '喂喂喂，再次输入下密码呀。' },
          { min: 8, message: '密码长度不能小于8位哦' },
        ]"
      >
        <a-input-password v-model:value="formState.checkPassword" placeholder="来，确认下密码吧"/>
      </a-form-item>
    <div class="tips">
      有账号？
      <router-link to="/user/login">去登录</router-link>
    </div>
      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%">注册</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import {useRouter} from "vue-router";
import {message} from "ant-design-vue";
import {userRegisterUsingPost} from "@/api/userController";

const formState = reactive<API.UserRegisterRequest>({
  userAccount: '',
  userPassword: '',
  checkPassword: '',
})

const router = useRouter()
const handleSubmit = async (values: API.UserRegisterRequest) => {
  // 判断两次输入密码是否一致
  if (formState.userPassword !== formState.checkPassword) {
    message.error('两次输入的密码不一致')
    return
  }
  const res = await userRegisterUsingPost(values)
  if (res.data.code === 0 && res.data.data) {
    message.success('注册成功')
    router.push({
      path: '/user/login',
      replace: true,
    })
  } else {
    message.error('注册失败：' + res.data.message)
  }
}
</script>
<style scoped>
#userRegisterPage {
  /* 设置登录页面最大宽度为360px */
  max-width: 360px;
  /* 水平居中 */
  margin: 0 auto;
}
.title {
  /* 标题文字居中 */
  text-align: center;
  /* 标题底部边距16px */
  margin-bottom: 16px;
}
.desc {
  /* 描述文字居中 */
  text-align: center;
  /* 描述文字颜色为#bbb */
  color: #bbb;
  /* 描述文字底部边距16px */
  margin-bottom: 16px;
}
.tips {
  /* 提示区域底部边距16px */
  margin-bottom: 16px;
  /* 提示文字颜色为#bbb */
  color: #bbb;
  /* 提示文字大小为13px */
  font-size: 13px;
  /* 提示文字右对齐 */
  text-align: right;
}
</style>
